<?php

namespace Admin\Controller;
use Think\Controller;
class WchatController extends Controller {
	private $id,$secret,$token;

	/*
	 * 用户登录,被动回复(默认)
	 * 
	 */
	public function _initialize(){
		header("Content-Type: text/html; charset=utf-8");
		$wechat= M('wechat')->where($_GET)->find();
		$this->token = $wechat['token']; 
		$this->secret=$wechat['appsecret'];
		$this->id=$wechat['appid'];
	}
	public function index(){

		$token = $this->token; 
		$appid=$this->id;
		$appsecret=$this->secret;
	    $wechat=new \Com\Wechat($token,$appid);
		$msg=$wechat->request();
		/*$wechat->replyText(json_encode($msg));*/
		if($msg['MsgType']){
			switch ($msg["MsgType"]){
				case 'event':
						/**
						 * 扫码发生关注事件
						 */
						$code=$msg['ToUserName'];
						if(($msg['Event']=='subscribe')&&($msg['EventKey'])){
							$msg_data=$msg['EventKey'];
							$leng=strlen($msg_data);
							$star=strpos($msg_data,"_");
							$str=substr($msg_data,$star+1,$leng);
						//	$data_end=json_decode($json,true);
							$re_mes=re_code($str,$code);
							$res=$wechat->replyText($re_mes);

						}
						elseif ($msg['Event']=='SCAN') {
							$re_mes="你已经扫过了点击注册".re_code($msg['EventKey'],$code);
							$res=$wechat->replyText($re_mes);

						}
						
					break;
				case "text":
		           $res=$wechat->replyText($msg['Content']);
					break;
				case "image":
		           $res=$wechat->replyImage($msg["MediaId"]);
					break;
				case "voice":
		            $res=$wechat->replyVoice($msg["MediaId"]);
					break;
				case "video":
		            $res=$wechat->replyVideo($msg["MediaId"]);
					break;
			 
				default:
					break;
			
			}
		}
		else{
			$res=$wechat->response(json_encode($msg));
		}


	}
	public function active(){
		$token = $this->token; 
		$appid=$this->id;
		$appsecret=$this->secret;
		dump(array($this->token,$this->id,$this->secret));
		$api=new \Com\WechatAuth($appid,$appsecret);

		$res=$api->new_jsapi_ticket();
		dump($res);
	}

	/*
	 * 用户关注公众号激活事件
	 * 
	 */
	public function  scanner(){
	   $wechat=D('Wchat')->reweichat();
       $mag=$wechat->serve();
       $this->code();
       $wechat->replay("欢迎关注粤海水利智能平台微信公众号/:,@-D");
	}
	  

	
	/*
	 * 微信获取code换取当前用户信息
	 * Enter description here ...
	 */
    public  function code(){
	$APPID=$this->id;
	  $REDIRECT_URI='http://2508b1b1.nat123.cc/wx-tp/index.php/Admin/Wchat/getcode';
	//$scope='snsapi_base';//不需要授权
    $scope='snsapi_userinfo';//需要授权
	$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state='.$state.'wechat_redirect';
	header("Location:".$url);
    	
    }
    
    
   /*
    * 利用code获取access_token等个人用户信权
    */
      public function  getcode(){
		$appid=$this->id;
		$appsecret=$this->secret;
		$code = $_GET["code"]; 
		$get_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
			$ch = curl_init($get_token_url);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_HEADER, true); 
			$res=curl_exec($ch);
			curl_close($ch);
	        $res=strstr($res, "{");
	        $resulit=json_decode($res);
	  
	}



}